/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;


import l1j.opqlo.Server.L1World.WorldClan;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.CastleTable;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.item.L1ItemId;
import l1j.server.server.templates.L1Castle;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * TODO: 處理收到由客戶端傳來納稅的封包(?)
 */
public class C_Deposit extends ClientBasePacket {

	private static final String C_DEPOSIT = "[C] C_Deposit";
	private static final Logger _log = Logger.getLogger(C_Deposit.class.getName());
	@Override
	public String getType() {
		return C_DEPOSIT;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance player = client.getActiveChar();
			if (player == null) {
				return;
			}
			final int i = this.readD();
			final int j = this.readD();

			if (i == player.getId()) {
				final L1Clan clan = WorldClan.get().getClan(player.getClanname());
				if (clan != null) {
					final int castle_id = clan.getCastleId();
					if (castle_id != 0) { // 城主クラン
						final L1Castle l1castle = CastleTable.getInstance()
						.getCastleTable(castle_id);
						synchronized (l1castle) {
							int money = l1castle.getPublicMoney();
							if (player.getInventory()
									.consumeItem(L1ItemId.ADENA, j)) {
								money += j;
								l1castle.setPublicMoney(money);
								CastleTable.getInstance().updateCastle(l1castle);
							}
						}
					}
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}

}
